jsx3.lang.Class.defineClass("tibco.admin.Help", null, null, function(help) {


    help.registerHotKeyToServer = function() {
        help.hotkey = tibco.admin.Main.getServer().registerHotKey(help._onHelpKey, "F2", null, true, null)
    }

    help.invoke = function (objThis, arrArgs) {
        help.hotkey.invoke(objThis, arrArgs)

    }

    help._onHelpKey = function(objEvent) {
        var objJSX = jsx3.html.getJSXParent(objEvent.srcElement());
        tibco.admin.util.Log.SYSTEM.trace("tibco.admin.Help#_onHelpKey objJSX  = " + objJSX)
        if (!objJSX) objJSX = tibco.admin.Main.getServer().getBodyBlock();
        if (objJSX) help.onHelp(objJSX);
    };

    help.onHelp = function(objJSX) {
        var strId = null;
        var nameSpace = null
        while (objJSX && !strId) {
            strId = objJSX.helpid;
            nameSpace = objJSX.namespace
            tibco.admin.util.Log.SYSTEM.trace("tibco.admin.Help#onHelp objJSX = " + objJSX + " strId  = " + strId + " namespace = " + objJSX.namespace)
            objJSX = objJSX.getParent();

        }
        if (strId)  help.onContextHelp({subject:tibco.admin.Main.HELP, helpid:strId, namespace:nameSpace, model:objJSX});
        return Boolean(strId);
    };

    help.onContextHelp = function(objEvent) {
        var fromContextRoot = true;
        var namespace = objEvent.namespace;
        var helpid = objEvent.helpid
        tibco.admin.util.Log.SYSTEM.trace("tibco.admin.Help#onContextHelp helpid= " + helpid + "| namespace = " + namespace);
        //get the context first from context root
        var contextroot = "/"+tibco.admin.Main.getContextRootForNamespace(namespace)
        if(namespace == "tibco.admin"){
            contextroot = "/"+tibco.admin.Main.contextRoots["tibco.admin"]
        }
        var contextpath =   contextroot + "/help/context.xml"
        tibco.admin.util.Log.SYSTEM.trace("tibco.admin.Help#onContextHelp contextroot= " + contextroot);
        tibco.admin.util.Log.SYSTEM.trace("tibco.admin.Help#onContextHelp contextpath= " + contextpath);
        var xml = new jsx3.xml.CDF.Document().load(contextpath);



        // help doc still not ready or runing from file system
        if (xml.hasError()) {
            fromContextRoot = false;
            contextpath = "jsxaddin://user!MatrixAdmin/help/context.xml"
            xml = new jsx3.xml.CDF.Document().load(contextpath);
        }
        //var key = tibco.admin.Main.getServer().getLocale();
        if (!xml.hasError()) {
            var rec = xml.getRecord(helpid);
            if (!rec) {
                tibco.admin.util.Log.SYSTEM.warn("No context help entry for " + objEvent.helpid + ". Falling back to Matrix Admin.");
                rec = xml.getRecord("ID_TOP");
                namespace = "tibco.admin"
            }
            if (!namespace) {
                tibco.admin.util.Log.SYSTEM.warn("Missing namespace for " + objEvent.helpid + ". Falling back to Matrix Admin.");
                rec = xml.getRecord("ID_TOP");
                namespace = "tibco.admin"
            }
            if (rec) {
                //var path = rec.jsxtext;
                if (fromContextRoot) {
                    var abspath =   contextroot + "/help/"+ rec.jsxtext
                }
                else {
                    var abspath =   tibco.admin.Main.getServer().resolveURI(rec.jsxtext)
                }
                tibco.admin.util.Log.SYSTEM.trace("tibco.admin.Help#onContextHelp path to context help= " + abspath);
                var w = window.open(abspath, "jsxidectxhelp");
                if (!w) {
                    tibco.admin.util.Log.SYSTEM.warn("A pop-up blocker may have prevented context help from opening.");
                }
            }
        } else {
            tibco.admin.util.Log.SYSTEM.info(xml.getError());
            tibco.admin.Main.alert(tibco.admin.Main.getServer().getDynamicProperty("@Help Alert Dialog Title"),
                    tibco.admin.Main.getServer().getDynamicProperty("@Help Alert Dialog Label")+"\n contextpath = " +contextpath);
        }
    };
    help.registerHotKeyToServer();
})
